#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <Windows.h>
#include <QDebug>
#include <time.h>

#include "dumpthread.h"

#pragma comment(lib,"User32.lib")

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    memset ((void *)&updi, 0, sizeof(UPDINFO));
    handle = NULL;
    di = {0};
    updi = {0};

    devCheckThread = new DumpThread(this);
    devCheckThread->start();
}

MainWindow::~MainWindow()
{
    qDebug("exit");
    devCheckThread->exit();
    delete ui;
}

void MainWindow::on_connectButton_clicked()
{
    cnn_dev(&handle,&di,0,0);
    if (handle) {
        //get the basic information about device
        qDebug("(DBG) device code = %d.%d",di.hw_type_major,di.hw_type_minor);
        qDebug("(DBG) device boot loader version = %0x",di.bldr_ver);
        qDebug("(DBG) device application version : %x",di.app_ver);
        qDebug("(DBG) device language version : %x",di.lang_ver);
        qDebug("(DBG) device s/n : %s",di.sn );
        qDebug("(DBG) device hardware version = %c.%c",di.hw_ver [0],di.hw_ver [1]);
        qDebug("(DBG) device flash locked : %s",di.locked ? "true" : "false");

        //ping the device
//        double start,end ;
//        for (int i = 0 ; i < 10 ; i ++) {
//            start = clock();
//            ping_dev (handle,0);
//            end = clock();
//            qDebug("(DBG) PING %8d times , REPLY : %f ms", i,((end - start) / CLOCKS_PER_SEC) * 1000);
//        }

        //disconnect with device
    }
}

void MainWindow::on_disconnectButton_clicked()
{
    if(handle){
        discnn_dev(handle,0);
    }
}

void MainWindow::on_pingButton_clicked()
{
    double start,end ;
    start = clock();
    ping_dev (handle,0);
    end = clock();
    qDebug("(DBG) PING , REPLY : %f ms",((end - start) / CLOCKS_PER_SEC) * 1000);

//    emit reflashConnDevList(devInfoList);
}
